# -*- coding: utf-8 -*-
# @project: huawang-api20230914
# @file： tmp_lg_05.py
# @Time：2023/8/30 14:19
# @Author：liaogan
from asyncio import sleep

import allure
import selenium
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.chrome import webdriver
from selenium.webdriver.common.by import By

from common.commin.commin import read_ini
from common.log.log import log
from config.conf import BASE_DIR


"""环境url"""
yg_url = read_ini(BASE_DIR + '/config_yg.ini')['host']['url']  # 云购
yk_url = read_ini(BASE_DIR + '/config_yk.ini')['host']['url']  # 云客
test_yk_url = read_ini(BASE_DIR + '/config_yk.ini')['host']['test_url']  # 云客test环境
yc_url = read_ini(BASE_DIR + '/config_yc.ini')['host']['url']  # 云创
"""管理台账号密码"""
yg_username = read_ini(BASE_DIR + '/config_yg.ini')['host']['user']
yg_password = read_ini(BASE_DIR + '/config_yg.ini')['host']['password']
yc_username = read_ini(BASE_DIR + '/config_yc.ini')['host']['user']
yc_password = read_ini(BASE_DIR + '/config_yc.ini')['host']['password']
yk_username = read_ini(BASE_DIR + '/config_yk.ini')['host']['user']
yk_password = read_ini(BASE_DIR + '/config_yk.ini')['host']['password']


@allure.step('花王云购后台ui登录')
def yg_ui_login_backstage(mode=0):
    log.info('登录商城后台')
    global driver_chrome
    if mode == 0:
        log.info('远程docker容器启动')
        driver_chrome = selenium.webdriver.remote.webdriver.WebDriver(
            command_executor="http://166.166.200.50:5001/wd/hub",
            desired_capabilities=DesiredCapabilities.CHROME
        )
    elif mode == 1:
        log.info('本地启动谷歌浏览器')
        driver_chrome = webdriver.Chrome()
    # 初始参数配置
    driver_chrome.implicitly_wait(15)
    driver_chrome.delete_all_cookies()  # 为了防止污染，清空cookie
    driver_chrome.get(yg_url + '/admin/login')
    driver_chrome.maximize_window()
    sleep(0.5)
    driver_chrome.find_element(By.CSS_SELECTOR, '[name="username"]').send_keys(yg_username)
    driver_chrome.find_element(By.CSS_SELECTOR, '[name="pwd"]').send_keys(yg_password)
    sel = driver_chrome.find_elements(By.CSS_SELECTOR, '.el-input__inner')
    sel[-1].clear()
    sel[-1].send_keys('123456')
    driver_chrome.find_element(By.CSS_SELECTOR, '.el-button--medium').click()
    sleep(0.5)
    return driver_chrome


yg_ui_login_backstage()